/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktest.entities.InputEntity;

import java.awt.Font;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.util.InputAdapter;

import slicktest.constants.MapConstants;

import slicktest.entities.Drawable.Drawable;
import slicktest.entities.ImmobileEntity.Tower;
import slicktest.entities.maps.GameMap;

/**
 *
 * @author enrightt
 */
public class TowerContextMenu extends InputEntity implements Drawable {

    private boolean isVisible = false;
    private Tower tower;
    private final Color fillColor;
    private final Color borderColor;
    private final Color textColor;
    private final TrueTypeFont textFont;
    private final GameMap map;
    private float tlx;
    private float tly;
    private final int width;
    private final int height;
    private static final int FONT_SIZE = 18;

    public TowerContextMenu(Color fillColor, Color borderColor, Color textColor, GameMap map, int width, int height) {
        super();
        this.tower = null;
        this.fillColor = fillColor;
        this.borderColor = borderColor;
        this.textColor = textColor;
        this.textFont = new TrueTypeFont(new Font("ARIAL", Font.BOLD, FONT_SIZE), true);
        this.map = map;
        this.tlx = 0;
        this.tly = 0;
        this.width = width;
        this.height = height;
    }

    public String getType() {
        return "Tower Context Component";
    }

    public void update(GameContainer gc, int delta) throws SlickException {
    }

    @Override
    public void keyPressed(int key, char c) {
        if (key == Input.KEY_ESCAPE) {
            isVisible = false;
            System.out.println("isn't visible");
        }
    }

    @Override
    public void mouseClicked(int button, int x, int y, int clickCount) {
        int screenWidth = MapConstants.SCREEN_WIDTH;
        int screenHeight = MapConstants.SCREEN_HEIGHT;

        int tileWidth = MapConstants.TILE_WIDTH;
        int tileHeight = MapConstants.TILE_HEIGHT;

        //mimics the transform we do in GameMap to scroll the map around.
        int camScreenX = screenWidth / 2 - map.getCamX();
        int camScreenY = screenHeight / 2 - map.getCamY();

        //transform the mouse coordinates using the transform values we made
        int transX = x - camScreenX;
        int transY = y - camScreenY;

        int xPos = transX / tileWidth;
        int yPos = transY / tileHeight;

        tower = (Tower) map.getTower(xPos, yPos);

        if (tower != null) {
            this.tlx = x;
            this.tly = y;
            isVisible = true;
            System.out.println("is visible");
        }
    }

    public void render(GameContainer gc, Graphics graphics) throws SlickException {
        if (isVisible) {
            graphics.setColor(fillColor);
            graphics.fillRect(tlx, tly, width, height);

            graphics.setColor(borderColor);
            graphics.drawRect(tlx, tly, width, height);
        }
    }
}
